//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        perPageCount: 20, //每页显示个数 
        mySpecialList: [], //放置返回数据的数组 
        isFromFirst: true,   // 用于判断searchWorkList数组是不是空数组，默认true，空的数组  
        pageNo: 1,   // 设置加载的第几次，默认是第一次  
        isRefresh: false, //"上拉加载"的变量，默认false，隐藏  
        dataLoadingComplete: false,  //“没有数据”的变量，默认false，隐藏  
        isShowSkeletonLoding: true,
        everydayRecommend: [],
        goodsTypeList: [],
        mySpecialList: []
    },
    onLoad: function () {
        wx.getSystemInfo({
            success: function (res) {
                app.globalData['statusBarHeight'] = res.statusBarHeight;
                console.log(res);
            }
        })
        if (!app.globalData.userID) {    //未检测到  wx.login是异步函数、此时可能获取不到值
            //创建 app.plumSessionCallback 的回调函数，将plumSession作为函数参数，app.js中检测到函数被创建，
            app.plumSessionCallback = userID => {
                if (userID != '' && userID != null) {
                    if (app.globalData.userInfo.isLogin == 1) {
                        this.getHomePageData(this.data.pageNo);
                    } else {
                        wx.redirectTo({
                            url: '../login/loginPhone',
                        })
                    }
                } else {
                    app.weChatLogin();
                }
            }
        } else {
            if (app.globalData.userInfo.isLogin == 1) {
                this.getHomePageData(this.data.pageNo);
            } else {
                wx.redirectTo({
                    url: '../login/loginPhone',
                })
            }
        }
    },
    onShareAppMessage: function (res) {
        
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let that = this;
        if (that.data.isRefresh && !that.data.dataLoadingComplete) {
            var _pageNo = that.data.pageNo + 1;
            that.setData({
                pageNo: _pageNo,  //每次触发上拉事件，把pageNo+1  
                isFromFirst: false  //触发到上拉事件，把isFromSearch设为为false  
            });
            that.getHomePageData(_pageNo);
        }
    },
    toGoodsTypePage: function(e) {
        var _type = e.currentTarget.dataset.type;
        var info = e.currentTarget.dataset.info;
        if (_type == 4) {
            wx.navigateTo({
                url: '../seckill/seckill',
            })
        } else {
            wx.navigateTo({
                url: '../goodsList/goodsList?goodsTypeID=' + info.goodsTypeID + '&goodsTypeName=' + info.goodsTypeName,
            })
        }
    },
    getHomePageData: function (_pageNo) {
        var that = this;
        // request
        var params = {
            userID: app.globalData.userID,
            type: 0,
            pageNo: _pageNo
        }
        app.network.requestPost(app.route.ROUTE_GET_HOME_PAGE_DATA, params, '正在获取数据', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                var _everydayRecommend = res.context.everydayRecommend;
                var _goodsTypeList = res.context.goodsTypeList;
                var _mySpecialList = res.context.mySpecialList;
                if (_pageNo == 1) {
                    that.setData({
                        isShowSkeletonLoding: false,
                        everydayRecommend: _everydayRecommend,
                        goodsTypeList: _goodsTypeList,
                    });
                }
                let list = [];
                if (_mySpecialList.length != 0) {
                    that.data.isFromFirst ? list = res.context.mySpecialList : list = that.data.mySpecialList.concat(res.context.mySpecialList);
                    if (that.data.perPageCount > _mySpecialList.length) {
                        that.setData({
                            mySpecialList: list, //获取数据数组  
                            dataLoadingComplete: true, //把“没有数据”设为true，显示 
                            isRefresh: false
                        });
                    } else {
                        that.setData({
                            mySpecialList: list, //获取数据数组  
                            isRefresh: true   //把"上拉加载"的变量设为false，显示  
                        });
                    }
                } else {
                    if (_pageNo == 1) {
                        that.setData({
                            mySpecialList: list, //获取数据数组 
                            dataLoadingComplete: true //把“没有数据”设为true，显示 
                        });
                    } else {
                        that.setData({
                            mySpecialList: that.data.mySpecialList, //获取数据数组 
                            dataLoadingComplete: true, //把“没有数据”设为true，显示  
                            isRefresh: false  //把"上拉加载"的变量设为false，隐藏  
                        });
                    }
                }
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    toGoodsDetail: function(e) {
        var _goodsId = e.currentTarget.dataset.goodsid;
        wx.navigateTo({
            url: '../goodsDetail/goodsDetail?goodsId=' + _goodsId,
        })
    },
    bannerTap: function(e) {
        var _info = e.currentTarget.dataset.info;
        if (_info.type == 1) { //为1时跳转到商品详情页面 其他则是自定义链接
            wx.navigateTo({
                url: '../goodsDetail/goodsDetail?goodsId=' + _info.goodsId,
            })
        } else {
            wx.navigateTo({
                url: '../webPage/webPage?url=' + encodeURIComponent(_info.url)
            })
        }
    },

    goSearchTab: function() {
        wx.navigateTo({
            url: '../search/search'
        })
    }
})
